package tsinghua.smxq.mc;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.PoiOverlay;

public class MapSearchActivity extends MapActivity {
	ImageButton mBtnSearch = null;
	Button backToMain;
	MapView mMapView = null;
	MKSearch mSearch = null;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mapsearch);

		BaiduMap app = (BaiduMap) this.getApplication();
		if (app.bMapMan == null) {
			app.bMapMan = new BMapManager(getApplication());
			app.bMapMan.init(app.myKey, new BaiduMap.MyGeneralListener());
		}
		app.bMapMan.start();
		super.initMapActivity(app.bMapMan);

		mMapView = (MapView) findViewById(R.id.bmapView);
		mMapView.setBuiltInZoomControls(true);
		mMapView.setDrawOverlayWhenZooming(true);
		ZoomControls zoomControls = (ZoomControls) mMapView.getChildAt(2);
		zoomControls.setPadding(0, 0, 20, 80);

		mSearch = new MKSearch();
		mSearch.init(app.bMapMan, new MKSearchListener() {

			public void onGetPoiResult(MKPoiResult res, int type, int error) {
				if (error != 0 || res == null) {
					Toast.makeText(MapSearchActivity.this, "抱歉，未找到结果",
							Toast.LENGTH_LONG).show();
					return;
				}

				if (res.getCurrentNumPois() > 0) {
					PoiOverlay poiOverlay = new PoiOverlay(
							MapSearchActivity.this, mMapView);
					poiOverlay.setData(res.getAllPoi());
					mMapView.getOverlays().clear();
					mMapView.getOverlays().add(poiOverlay);
					mMapView.invalidate();
					mMapView.getController().animateTo(res.getPoi(0).pt);
				} else if (res.getCityListNum() > 0) {
					String strInfo = "在";
					for (int i = 0; i < res.getCityListNum(); i++) {
						strInfo += res.getCityListInfo(i).city;
						strInfo += ",";
					}
					strInfo += "找到结果";
					Toast.makeText(MapSearchActivity.this, strInfo,
							Toast.LENGTH_LONG).show();
				}
			}

			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
			}

			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
			}

			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
			}

			public void onGetAddrResult(MKAddrInfo res, int error) {
			}

			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}

		});

		mBtnSearch = (ImageButton) findViewById(R.id.search);

		mBtnSearch.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SearchButtonProcess(v);
			}
		});

		backToMain = (Button) findViewById(R.id.mapsearch_back);
		backToMain.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MapSearchActivity.this.startActivity(new Intent(
						MapSearchActivity.this, MobileCityActivity.class));
				MapSearchActivity.this.overridePendingTransition(
						R.anim.push_in, R.anim.push_out);
				MapSearchActivity.this.finish();
			}

		});

	}

	void SearchButtonProcess(View v) {
		if (mBtnSearch.equals(v)) {
			EditText editCity = (EditText) findViewById(R.id.city);
			EditText editSearchKey = (EditText) findViewById(R.id.searchkey);
			mSearch.poiSearchInCity(editCity.getText().toString(),
					editSearchKey.getText().toString());
		}
	}

	@Override
	protected void onPause() {
		BaiduMap app = (BaiduMap) this.getApplication();
		app.bMapMan.stop();
		super.onPause();
	}

	@Override
	protected void onResume() {
		BaiduMap app = (BaiduMap) this.getApplication();
		app.bMapMan.start();
		super.onResume();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
